<%@ page contentType="text/html;charset=UTF-8" language="java" import=" com.scalar.chiptrack.utils.ConfigManager, java.util.HashMap"%>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<chiptrack:checkLogon sourcePage="/purchaseOrder.do"/>

<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>

<HTML><HEAD><TITLE>Scalarsoft - ChipTrac</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type="text/css" rel="stylesheet">

<META content="MSHTML 5.50.4807.2300" name=GENERATOR>
<script src="<%=chipTracContext%>/jsp/includes/scripts/functions.js">
</script>
<SCRIPT language="JavaScript" src="<%=chipTracContext%>/jsp/includes/scripts/validations.js">
</script>
<SCRIPT LANGUAGE="JavaScript">
<%
	HashMap errors = (HashMap) request.getAttribute( "error" );
	String closeWindow = (String)request.getAttribute( "closeWindow" );
	
	//System.out.println( "close Window value  "+ closeWindow );

	if( closeWindow != null && closeWindow.equalsIgnoreCase( "TRUE" ) && ( errors == null || errors.size() < 1 ))
	{
		//System.out.println( "I am closing the Window" );
%>
	window.close();
<%
	}
	String purchaseOrderNumber = null;
	String purchaseOrderSequenceNumber = null;
	int maxQtyToPull = 0;
	int rowIndex = 0;

	//if( !( closeWindow != null && closeWindow.equalsIgnoreCase( "TRUE" ) && ( errors == null || errors.size() < 1 ) ) )
	//{
		purchaseOrderNumber = request.getParameter( "poNumber" );
		purchaseOrderSequenceNumber =  request.getParameter( "poSeqNumber" );
		if( request.getParameter( "maxQty" ) != null )
		maxQtyToPull = Integer.parseInt( request.getParameter( "maxQty" ) );
		if ( request.getParameter( "maxQty" ) != null )
		rowIndex = Integer.parseInt( request.getParameter( "rowIndex" ) );
	//}
%>

var purchaseOrder = "<%=purchaseOrderNumber%>";
var maxQty = "<%=maxQtyToPull%>";
var rowIndex = "<%=rowIndex%>";
var count = 0;
function enableFields()
{
    for ( var i = 0; i < count;  i++ )
    {
        if ( document.PurchaseOrderLotForm['lotList['+i+'].lotSelect'].checked == true )
        {
            document.PurchaseOrderLotForm['lotList['+i+'].lotPulled'].disabled = false;
            //document.PurchaseOrderLotForm['lotList['+i+'].poNumber'].disabled = false;
			document.PurchaseOrderLotForm['lotList['+i+'].poNumber'].value = purchaseOrder;
        }
        else
        {
            document.PurchaseOrderLotForm['lotList['+i+'].lotPulled'].disabled = true;
            document.PurchaseOrderLotForm['lotList['+i+'].poNumber'].disabled = true;
        }
    }
}

function enableField( i )
{
	if ( document.PurchaseOrderLotForm['lotList['+i+'].lotSelect'].checked == true )
	{
		document.PurchaseOrderLotForm['lotList['+i+'].lotPulled'].disabled = false;
		document.PurchaseOrderLotForm['lotList['+i+'].poNumber'].value = purchaseOrder;
	}
	else
	{
		document.PurchaseOrderLotForm['lotList['+i+'].lotPulled'].disabled = true;
		document.PurchaseOrderLotForm['lotList['+i+'].lotPulled'].value = 0; 

		document.PurchaseOrderLotForm['lotList['+i+'].poNumber'].value = document.PurchaseOrderLotForm['lotList['+i+'].poNumber'].defaultValue;
		document.PurchaseOrderLotForm['lotList['+i+'].poNumber'].disabled = true;
	}
}

function restrictCharecters()
{
    if( event.keyCode < 48 || event.keyCode > 57 )
    {
        event.keyCode = false;
    }
}

function submitScreen()
{
	var flag	= false;
    var errors	= "";
    var pa		= document.getElementById("errors");
	var totalPulledQty = 0;
    pa.innerHTML = "";

    for ( var i = 0; i < count;  i++ )
    {
        if ( document.PurchaseOrderLotForm['lotList['+i+'].lotSelect'].checked == true )
        {
			flag   = true;
			document.PurchaseOrderLotForm['lotList['+i+'].lotPulled'].value = document.PurchaseOrderLotForm['lotList['+i+'].lotPulled'].value.trim();

			document.PurchaseOrderLotForm['lotList['+i+'].poNumber'].value = document.PurchaseOrderLotForm['lotList['+i+'].poNumber'].value.trim();

			if ( document.PurchaseOrderLotForm['lotList['+i+'].lotPulled'].value != '' )
			{
				document.PurchaseOrderLotForm['lotList['+i+'].lotPulled'].value = Number(document.PurchaseOrderLotForm['lotList['+i+'].lotPulled'].value);
			}

            if ( document.PurchaseOrderLotForm['lotList['+i+'].lotPulled'].value == "" || document.PurchaseOrderLotForm['lotList['+i+'].lotPulled'].value == "0" )
            {
                errors = "<li> Lot Pulled Quantity Cannot be empty (OR) zero when lot is selected";
            }
            else if ( Number( document.PurchaseOrderLotForm['lotList['+i+'].lotPulled'].value ) > Number( document.PurchaseOrderLotForm['lotList['+i+'].lotQty'].value ) )
            {
                errors = "<li> Lot Quantity Cannot be Greater then Lot Quantity";
                document.PurchaseOrderLotForm['lotList['+i+'].lotPulled'].value = document.PurchaseOrderLotForm['lotList['+i+'].lotPulled'].defaultValue;
            }
            if ( (document.PurchaseOrderLotForm['lotList['+i+'].poNumber'].value) == "" )
            {
                errors = errors+"<br><li> PO Number Cannot be empty when lot is selected";
            }
			totalPulledQty = totalPulledQty + parseInt( document.PurchaseOrderLotForm['lotList['+i+'].lotPulled'].value );
        }
        if( errors != "" )
        {
            break;
        }
    }

	if( totalPulledQty > parseInt( maxQty ) )
	{
		errors = "<li> Total Quantity pulled : " + totalPulledQty + " is greater than the Quantity to be Pulled : "+ maxQty;
	}

	if( flag == false )
	{
		errors = "<li> Atleast one Lot must be selected (OR) click Cancel to go back";
	}

    if( errors == "" )
    {
        for ( var i = 0; i < count;  i++ )
        {
			document.PurchaseOrderLotForm['lotList['+i+'].lotNumber'].disabled	= false;
			document.PurchaseOrderLotForm['lotList['+i+'].lotQty'].disabled		= false;
			document.PurchaseOrderLotForm['lotList['+i+'].lotPulled'].disabled		= false;
			document.PurchaseOrderLotForm['lotList['+i+'].poNumber'].disabled	= false;
        }		
        document.PurchaseOrderLotForm.action = '<%=chipTracContext%>/purchaseOrderLotAction.do?action=savePoLots';
	    document.PurchaseOrderLotForm.submit();
		window.opener.setQuantityPulled( totalPulledQty, rowIndex );
		// window.close();
    }
    else
    {
        pa.innerHTML = errors;
		location="#a";
    } 
}

function reset()
{
    document.PurchaseOrderLotForm.reset();
    enableFields();
}
</SCRIPT>
</HEAD>
<BODY bgColor="#ffffff" leftMargin=0 topMargin=0 marginheight="0" marginwidth="0" onload="javascript:enableFields()">
<table cellSpacing="0" cellPadding="0" width="100%" border="0">
	<tr>
<!-- 		<td width='180' valign='top' background='<%=chipTracContext%>/jsp/includes/images/left_red_bg.gif'>
		</td> -->
		<td bgcolor="#ffffff"><IMG height=9 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8></td>
		<td bgcolor="#ffffff"><IMG height=480 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=2></td>
		<td valign="top">
			<table cellSpacing="0" cellPadding="0" width="100%" border="0">
				<tr><td class="NArialW" width='100%' align="center" valign="top">&nbsp;</td></tr>
				<tr><td class="NArialW" width='100%' align="center" valign="top">Purchase Order Lots</td></tr>
				<tr><td align="left"><chiptrack:errors styleClass="NArialL"/></td></tr>
				<tr><td align="left"><chiptrack:info styleClass="NArialL"/></td></tr>
				<tr>
					<td width="100%" valign="top">
					<html:form name="PurchaseOrderLotForm" type="com.scalar.chiptrack.finance.purchaseorder.forms.PurchaseOrderLotForm" action="/purchaseOrderLotAction.do" scope="session">
					<table border="0" width="100%" align=right>
						<tr><td><a href="#a"></a></td></tr>
                        <tr><td colspan='6' align="left" class='NArialL'>&nbsp;<FONT id="errors" styleClass="NArialL" color='FF0000'></FONT></TD></TR>
						<tr>
							<td width="100%">
								<%
									boolean results = false;
								%>
								<table border="0" width="100%" align="center">
								<nested:iterate name="PurchaseOrderLotForm" property="lotList"  type="com.scalar.chiptrack.finance.purchaseorder.PurchaseOrderLot" length="1">
									<% results = true; %>
								</nested:iterate>
									<%
									if( !results )
									{
									%>
									<tr><td colspan="5" class="NArialL"><font color='FF0000'><B>No Lots available for the Part Number and Vendor</B></font></td></tr>
									<%
									}
									%>
									<tr><td colspan="5">&nbsp;</td></tr>
									<tr>
										<td colspan="5" width="90%" align="center">
											<table border="0" width="100%" align="center">
												<% 
												if( results ) 
												{
												%>
												<tr bgcolor="#993333">
                                                    <logic:notEqual parameter="action" value="poLotsModify">
													<td width="5%" align="center" class="NArialL">
														<font color="#FFFFFF">Select</font>
													</td>
                                                    </logic:notEqual>
													<td width="30%" align="center" class="NArialL">
														<font color="#FFFFFF">PO Number</font>
													</td>
													<td width="30%" align="center" class="NArialL">
														<font color="#FFFFFF">Lot Number</font>
													</td>
                                                    <logic:notEqual parameter="action" value="poLotsModify">
													<td width="15%" align="center" class="NArialL">
														<font color="#FFFFFF">Lot Quantity</font>
													</td>
                                                    </logic:notEqual>
													<td width="15%" align="center" class="NArialL">
														<font color="#FFFFFF">Qty Pulled</font>
													</td>
												</tr>
												<%
												}
												else
												{
												%>
												<tr><td colspan=5>&nbsp;</td></tr>
												<%
												}
												%>
												
												<%
													int iterateIdx = 1;
												%>
												<nested:iterate name="PurchaseOrderLotForm" property="lotList" id="xyz" type="com.scalar.chiptrack.finance.purchaseorder.PurchaseOrderLot">
													<% 														
													if( iterateIdx++ % 2 == 0 ){ %>
													<tr bgcolor='#FFFFEE'>
													<%}else{%>
													<tr bgcolor='#DEE1E2'>
													<%}%>
                                                    <logic:notEqual parameter="action" value="poLotsModify">
													<td width="5%" align="center">
														<%
															String str = "javascript:enableField(" + (iterateIdx - 2) + ")";
														%>
													    <nested:checkbox property="lotSelect" onclick="<%=str%>"/>
													</td>
                                                    </logic:notEqual>
                                                    <logic:equal parameter="action" value="poLotsModify">
													    <nested:hidden property="lotSelect" />
                                                    </logic:equal>
													<td width="30%" align="center">
														<nested:text property="poNumber" size="17" maxlength="17" styleClass="txfield" disabled="true" />
														<nested:hidden property="poSeqNumber" value="<%=purchaseOrderSequenceNumber%>"/>
														<nested:hidden property="vendorName" />
														<nested:hidden property="vendorNumber"/>
														<nested:hidden property="partNumber"/>
														<nested:hidden property="ps_type"/>
														<nested:hidden property="ps_type_name"/>
													</td>
													<td width="30%" align="center">
														<nested:text property="lotNumber" size="21" maxlength="21" styleClass="txfield" disabled="true"/>
														<nested:hidden property="lotId"/>
													</td>
                                                    <logic:notEqual parameter="action" value="poLotsModify">
													<td width="15%" align="center">
														<nested:text property="lotQty" size="8" maxlength="8" styleClass="txfield" onkeypress="javascript:restrictCharecters()" disabled="true"/>
													</td>
                                                    </logic:notEqual>
													<td width="15%" align="center">
														<nested:text property="lotPulled" size="8" onkeypress="javascript:restrictCharecters()" maxlength="8" styleClass="txfield" disabled="true"/>
													</td>
												</tr>
												</nested:iterate>
                                                <script>
                                                    count = '<%= (iterateIdx-1) %>';
                                                 </script>
											</table>
										</td>
									</tr>
									<tr><td colspan="6" width="100%">&nbsp;</td></tr>
									<tr>
										<td colspan="6" align="center">
											<%
											if( results )
											{ 
											%>
                                            <logic:notEqual parameter="action" value="poLotsModify">
											<a href="javascript:submitScreen();"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/save.gif" width="53" height="17" onmouseover="window.status='Click here to save';return true" onmouseout="window.status='';return true" title="Click here to save"></a> &nbsp;&nbsp;
											<a href="javascript:reset();"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/reset.gif" width="53" height="17" onmouseover="window.status='Click to Reset';return true" onmouseout="window.status='';return true" title="Click here to Reset"></a>&nbsp;&nbsp;
                                            </logic:notEqual>
											<%
											}
											%>
                                            <a href="javascript:window.close()" onmouseover="window.status='Click here to go back Purchase Orders Menu';return true" onmouseout="window.status='';return true"><IMG SRC="<%=chipTracContext%>/jsp/includes/images/cancel.gif" title="Click here to Cancel" ALIGN="BOTTOM" BORDER="0"></a>
										</td>
									</tr>
								</table>
							</td>
						</tr>
					</table>
					</html:form>
					</td>
					<td bgcolor="#ffffff"><IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=1></td>
				</tr>
			</table>
		</td>
	</tr>
</table>
</BODY>
</HTML>
